import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);

  // 能力模板数据
  const capabilityTemplates = [
    {
      id: '1',
      name: '酒店客房管理模板',
      code: 'HOTEL_ROOM_MANAGEMENT_TEMPLATE',
      description: '酒店客房管理的标准能力模板',
      type: 'business',
      category: 'hotel',
      version: '1.0.0',
      status: 'active',
      icon: 'ant-design:home-outlined',
      color: '#52c41a',
      tags: ['酒店', '客房管理', '标准模板'],
      capabilities: [
        {
          id: 'room_status_management',
          name: '房态管理',
          code: 'ROOM_STATUS_MANAGEMENT',
          description: '客房状态管理能力',
          type: 'workflow',
          priority: 95,
        },
        {
          id: 'room_cleaning_management',
          name: '清洁管理',
          code: 'ROOM_CLEANING_MANAGEMENT',
          description: '客房清洁管理能力',
          type: 'workflow',
          priority: 85,
        },
        {
          id: 'room_maintenance_management',
          name: '维护管理',
          code: 'ROOM_MAINTENANCE_MANAGEMENT',
          description: '客房维护管理能力',
          type: 'workflow',
          priority: 80,
        },
      ],
      parameters: [
        {
          name: 'hotelId',
          type: 'string',
          description: '酒店ID',
          required: true,
        },
        {
          name: 'roomCount',
          type: 'number',
          description: '客房数量',
          required: true,
          defaultValue: 100,
        },
        {
          name: 'enableIoT',
          type: 'boolean',
          description: '启用IoT设备',
          required: false,
          defaultValue: true,
        },
      ],
      usage: {
        totalInstances: 25,
        activeInstances: 23,
        averageRating: 4.8,
        lastUsed: '2024-01-15T10:30:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-15T14:20:00Z',
    },
    {
      id: '2',
      name: '温泉服务管理模板',
      code: 'SPA_SERVICE_MANAGEMENT_TEMPLATE',
      description: '温泉服务的标准能力模板',
      type: 'business',
      category: 'spa',
      version: '1.0.0',
      status: 'active',
      icon: 'ant-design:heart-outlined',
      color: '#eb2f96',
      tags: ['温泉', '服务管理', '标准模板'],
      capabilities: [
        {
          id: 'spa_booking_management',
          name: '温泉预订管理',
          code: 'SPA_BOOKING_MANAGEMENT',
          description: '温泉预订管理能力',
          type: 'workflow',
          priority: 90,
        },
        {
          id: 'spa_service_delivery',
          name: '服务交付',
          code: 'SPA_SERVICE_DELIVERY',
          description: '温泉服务交付能力',
          type: 'workflow',
          priority: 85,
        },
        {
          id: 'spa_quality_control',
          name: '质量控制',
          code: 'SPA_QUALITY_CONTROL',
          description: '温泉服务质量控制能力',
          type: 'workflow',
          priority: 80,
        },
      ],
      parameters: [
        {
          name: 'spaId',
          type: 'string',
          description: '温泉ID',
          required: true,
        },
        {
          name: 'serviceTypes',
          type: 'array',
          description: '服务类型',
          required: true,
          defaultValue: ['massage', 'sauna', 'hot_spring'],
        },
        {
          name: 'maxCapacity',
          type: 'number',
          description: '最大容量',
          required: true,
          defaultValue: 50,
        },
      ],
      usage: {
        totalInstances: 12,
        activeInstances: 11,
        averageRating: 4.6,
        lastUsed: '2024-01-14T16:45:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-14T11:30:00Z',
    },
    {
      id: '3',
      name: '景区票务管理模板',
      code: 'SCENIC_TICKET_MANAGEMENT_TEMPLATE',
      description: '景区票务管理的标准能力模板',
      type: 'business',
      category: 'scenic',
      version: '1.0.0',
      status: 'active',
      icon: 'ant-design:environment-outlined',
      color: '#13c2c2',
      tags: ['景区', '票务管理', '标准模板'],
      capabilities: [
        {
          id: 'ticket_sales_management',
          name: '票务销售管理',
          code: 'TICKET_SALES_MANAGEMENT',
          description: '票务销售管理能力',
          type: 'workflow',
          priority: 95,
        },
        {
          id: 'visitor_management',
          name: '游客管理',
          code: 'VISITOR_MANAGEMENT',
          description: '游客管理能力',
          type: 'workflow',
          priority: 90,
        },
        {
          id: 'scenic_area_monitoring',
          name: '景区监控',
          code: 'SCENIC_AREA_MONITORING',
          description: '景区监控能力',
          type: 'workflow',
          priority: 85,
        },
      ],
      parameters: [
        {
          name: 'scenicId',
          type: 'string',
          description: '景区ID',
          required: true,
        },
        {
          name: 'ticketTypes',
          type: 'array',
          description: '票种类型',
          required: true,
          defaultValue: ['adult', 'child', 'senior', 'student'],
        },
        {
          name: 'maxDailyVisitors',
          type: 'number',
          description: '日最大游客数',
          required: true,
          defaultValue: 10_000,
        },
      ],
      usage: {
        totalInstances: 8,
        activeInstances: 7,
        averageRating: 4.7,
        lastUsed: '2024-01-13T12:15:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-13T09:45:00Z',
    },
    {
      id: '4',
      name: '餐饮服务管理模板',
      code: 'RESTAURANT_SERVICE_MANAGEMENT_TEMPLATE',
      description: '餐饮服务的标准能力模板',
      type: 'business',
      category: 'restaurant',
      version: '1.0.0',
      status: 'active',
      icon: 'ant-design:coffee-outlined',
      color: '#fa8c16',
      tags: ['餐饮', '服务管理', '标准模板'],
      capabilities: [
        {
          id: 'restaurant_booking_management',
          name: '餐厅预订管理',
          code: 'RESTAURANT_BOOKING_MANAGEMENT',
          description: '餐厅预订管理能力',
          type: 'workflow',
          priority: 90,
        },
        {
          id: 'menu_management',
          name: '菜单管理',
          code: 'MENU_MANAGEMENT',
          description: '菜单管理能力',
          type: 'workflow',
          priority: 85,
        },
        {
          id: 'kitchen_order_management',
          name: '厨房订单管理',
          code: 'KITCHEN_ORDER_MANAGEMENT',
          description: '厨房订单管理能力',
          type: 'workflow',
          priority: 95,
        },
      ],
      parameters: [
        {
          name: 'restaurantId',
          type: 'string',
          description: '餐厅ID',
          required: true,
        },
        {
          name: 'cuisineType',
          type: 'string',
          description: '菜系类型',
          required: true,
          defaultValue: 'chinese',
        },
        {
          name: 'maxCapacity',
          type: 'number',
          description: '最大容量',
          required: true,
          defaultValue: 200,
        },
      ],
      usage: {
        totalInstances: 15,
        activeInstances: 14,
        averageRating: 4.5,
        lastUsed: '2024-01-15T18:30:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-12T16:20:00Z',
    },
    {
      id: '5',
      name: '医美服务管理模板',
      code: 'MEDICAL_BEAUTY_SERVICE_TEMPLATE',
      description: '医美服务的标准能力模板',
      type: 'business',
      category: 'medical_beauty',
      version: '1.0.0',
      status: 'active',
      icon: 'ant-design:medicine-box-outlined',
      color: '#722ed1',
      tags: ['医美', '服务管理', '标准模板'],
      capabilities: [
        {
          id: 'medical_consultation_management',
          name: '医疗咨询管理',
          code: 'MEDICAL_CONSULTATION_MANAGEMENT',
          description: '医疗咨询管理能力',
          type: 'workflow',
          priority: 95,
        },
        {
          id: 'treatment_plan_management',
          name: '治疗方案管理',
          code: 'TREATMENT_PLAN_MANAGEMENT',
          description: '治疗方案管理能力',
          type: 'workflow',
          priority: 90,
        },
        {
          id: 'medical_record_management',
          name: '病历管理',
          code: 'MEDICAL_RECORD_MANAGEMENT',
          description: '病历管理能力',
          type: 'workflow',
          priority: 85,
        },
      ],
      parameters: [
        {
          name: 'clinicId',
          type: 'string',
          description: '诊所ID',
          required: true,
        },
        {
          name: 'serviceTypes',
          type: 'array',
          description: '服务类型',
          required: true,
          defaultValue: ['consultation', 'treatment', 'surgery'],
        },
        {
          name: 'maxPatients',
          type: 'number',
          description: '最大患者数',
          required: true,
          defaultValue: 100,
        },
      ],
      usage: {
        totalInstances: 6,
        activeInstances: 5,
        averageRating: 4.9,
        lastUsed: '2024-01-15T20:15:00Z',
      },
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-11T14:50:00Z',
    },
  ];

  // 过滤逻辑
  let filteredTemplates = capabilityTemplates;

  if (query.keyword) {
    const keyword = query.keyword as string;
    filteredTemplates = filteredTemplates.filter(
      (template) =>
        template.name.includes(keyword) ||
        template.description.includes(keyword) ||
        template.code.includes(keyword),
    );
  }

  if (query.type) {
    filteredTemplates = filteredTemplates.filter(
      (template) => template.type === query.type,
    );
  }

  if (query.category) {
    filteredTemplates = filteredTemplates.filter(
      (template) => template.category === query.category,
    );
  }

  if (query.status) {
    filteredTemplates = filteredTemplates.filter(
      (template) => template.status === query.status,
    );
  }

  // 分页
  const page = Number.parseInt(query.page as string) || 1;
  const pageSize = Number.parseInt(query.pageSize as string) || 10;
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  const items = filteredTemplates.slice(start, end);

  return {
    code: 0,
    data: {
      items,
      total: filteredTemplates.length,
      page,
      pageSize,
    },
    msg: '获取能力模板列表成功',
  };
});
